import ezdxf
import openpyxl

# 打开 Excel 文件
wb = openpyxl.load_workbook("test.xlsx")

# 获取特定列的数据
sheet = wb.active
column_data = [cell.value for cell in sheet["A"]]
print(column_data)
# 提取点坐标
for cell in sheet["A"]:
    data = cell.value
    print(data)

# 解析数据
data_list = data.split(",")
total_points = int(data_list[1])
elevation_start = int(data_list[-5])
elevation_end = int(data_list[-4])

points = []
for i in range(2, len(data_list)-5, 3):
    point_num = int(data_list[i])
    x = int(data_list[i+1])
    y = int(data_list[i+2])
    points.append((point_num, x, y))

# 投影点坐标
projected_points = [(point_num, x, y, elevation_start) for point_num, x, y in points]

# 连接点成线
lines = [(projected_points[i][1:], projected_points[i+1][1:]) for i in range(len(projected_points)-1)]

# 创建CAD文件
doc = ezdxf.new("R2010")
msp = doc.modelspace()

for line in lines:
    msp.add_line(line[0][0], line[0][1], line[0][2], line[1][0], line[1][1], line[1][2])

cad_filename = "output.dxf"
doc.saveas(cad_filename)

print(f"CAD文件已生成：{cad_filename}")

import ezdxf

# 创建Polyline对象
polyline = ezdxf.Polyline()

# 添加顶点
polyline.append((0, 0))
polyline.append((10, 0))
polyline.append((10, 10))
polyline.append((0, 10))

# 闭合线段
polyline.close()

# 导出DXF文件
polyline.export("polyline.dxf")
